#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int n;
  cin >> n;
  vector<pair<i64, int>> a(n + 1);
  for (int i = 1; i <= n; i ++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  sort(a.begin() + 1, a.end());

  vector<vector<i64>> dp(n + 1, vector<i64>(n + 1, 0));
  for (int l = n; l >= 1; l --) {
    dp[l][l] = a[1].first * abs(a[1].second - l);
    for (int r = l + 1; r <= n; r ++) {
      int len = r - l + 1;
      dp[l][r] = max(
        dp[l + 1][r] + a[len].first * abs(a[len].second - l),
        dp[l][r - 1] + a[len].first * abs(a[len].second - r)
      );
    }
  }
  cout << dp[1][n] << endl;
}